<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Request;
use EasyWeChat\Foundation\Application;
use addons\wechat\library\Config as ConfigService;

class Business extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        $this->model = new \app\common\model\Business();
        parent::_initialize();
    }

    public function index()
    {
        $area_id = $this->request->param('area_id');
        $brand_id = $this->request->param('brand_id');
        $keywords = $this->request->param('keywords');

        $where = [];
        if(isset($area_id)) {
            $area_id= intval($area_id);
            if($area_id) {
                $where['area_id'] = $area_id;
            }
        }else{
            $area_id = session('area_id');
            $where['area_id'] = $area_id;
        }


        if(isset($brand_id)) {
            $where['brand_id'] = intval($brand_id);
            $this->assign('brand_id', $brand_id);
        }
        if(isset($keywords)) {
            $where['name'] = ['like', '%' . $keywords . '%'];
        }
        $list = Db::name('business')->field('id,logo,name')->where($where)->limit(5)->select();

        $this->assign('list', $list);
        $this->assign('area_id', $area_id);
        // $this->assign('title', '品牌');
        return $this->view->fetch();
    }

    public function info($id)
    {
        $uid = $this->auth->id;
        $mobile = Db::name('user')->where(['id' => $uid])->value('mobile');
        $info = Db::name('business')->where(['id' => $id])->find();
        $auth = Db::name('business_auth')->where(['business_id' => $id])->find();
        // $history_record = Db::name('history')->where([
        //     'uid' => $uid,
        //     'business_id' => $id,
        // ])->find();
        // if($history_record) {
        //     Db::name('history')->where([
        //         'uid' => $uid,
        //         'business_id' => $id,
        //         ])->update(['time' => time()]);
        // }else{
            Db::name('history')->insert([
                'uid' => $uid,
                'business_id' => $id,
                'time' => time()
            ]);
        // }

        $history = Db::name('history')
                    ->alias('h')
                    ->field('h.*,u.avatar,u.nickname')
                    ->join('__USER__ u', 'u.id=h.uid')
                    ->where(['business_id' => $id])
                    ->order('id desc')
                    ->limit(20)
                    ->order('id desc')
                    ->select();
        $call = Db::name('call')
                    ->alias('c')
                    ->field('c.*,u.avatar,u.mobile,u.nickname')
                    ->join('__USER__ u', 'u.id=c.uid')
                    ->where(['business_id' => $id])
                    ->order('id desc')
                    ->limit(20)
                    ->order('id desc')
                    ->select();
        $call_num = Db::name('call')
                    ->alias('c')
                    ->field('c.*,u.avatar,u.mobile')
                    ->join('__USER__ u', 'u.id=c.uid')
                    ->where(['business_id' => $id])
                    ->count();

        $product = Db::name('business_product')->where(['business_id' => $id])->order('id desc')->paginate(10);
        $page = $product->render();

        $history_num = Db::name('history')->where(['business_id' => $id])->count();
        if($info) {
            $info['banner'] = explode(',', $info['banner']);
        }
        $this->assign('info', $info);
        $this->assign('auth', $auth);
        $this->assign('history', $history);
        $this->assign('call', $call);
        $this->assign('history_num', $history_num);
        $this->assign('call_num', $call_num);
        $this->assign('mobile', $mobile);
        $this->assign('product', $product);
        $this->assign('page', $page);
        $this->assign('title', $info['name']);
        // 分享
        $app = new Application(ConfigService::load());
        $js = $app->js;
        $this->assign('js', $js);
        return $this->view->fetch();
    }

    public function call($business_id, $mobile)
    {
        Db::name('call')->insert([
            'uid' => $this->auth->id,
            'mobile' => $mobile,
            'business_id' => $business_id,
            'create_time' => time()
        ]);
        $this->success('ok');
    }

}
